<?php

class AdminModule extends CWebModule
{    
	public $homeUrl = 'admin/default/index';
    //public $theme = 'bootstrap';
    
	public function init()
	{
		// this method is called when the module is being created
		// you may place code here to customize the module or the application

		// import the module-level models and components
		$this->setImport(array(
			'admin.models.*',
			'admin.components.*',
		));
        
        $this->setComponent('user', array(
            'class'=>'CWebUser',
            'loginUrl'=>array('admin/default/login'),
            'returnUrl'=>array('/admin/default/index')
        ));
        
        Yii::app()->setComponents(array(
            'errorHandler'=>array(
                'errorAction'=>'admin/default/error'
            )            
        ));
        
        //Yii::app()->theme = $this->theme;
        
        Yii::app()->user->setStateKeyPrefix('_admin');
	}

	public function beforeControllerAction($controller, $action)
	{
		if(parent::beforeControllerAction($controller, $action))
		{
			// this method is called before any module controller action is performed
			// you may place customized code here
            $route = 'admin/' . $controller->id . '/' . $action->id;
           // echo $route;
            $publicPages = array(
                'admin/default/login',
                'admin/default/error',
            );
            if (Yii::app()->user->isGuest && !in_array($route, $publicPages)){            
                Yii::app()->getModule('admin')->user->loginRequired();
            }
            else return true;
		}
		else
			return false;
	}
}
